package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dataaccess.Userdao;
import entity.Users;

/**
 * Servlet implementation class Viewuser
 */
@WebServlet("/Viewuser")
public class Viewuser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Viewuser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userdao ud=new Userdao();
		List<Users> userlist=ud.getAll();
		String json="{\"users\":[";
		for (int i=0;i<userlist.size();i++) {
			json+="{\"name\":\""+userlist.get(i).getName()+"\"";
			json+=",\"gender\":\""+userlist.get(i).getGender()+"\"";
			json+=",\"password\":\""+userlist.get(i).getPassword()+"\"}";
			if(i<userlist.size()-1) json+=",";
			else json+="]}";
		}
		System.out.println(json);
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		out.write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
